<?php	
class  Locations extends CI_Controller{
	private $key = "AIzaSyAmubg2LaSEzHQmKhYcboKKBzYX9J1rtGM";
	private $baseurl_list = "https://maps.googleapis.com/maps/api/place/search/json?";
	private $baseurl_detail = "https://maps.googleapis.com/maps/api/place/details/json?reference=";
	
	public function __construct(){
		parent::__construct();
		$this->load->model('location_detail');
		$this->load->model('events');
		$this->load->model('address_components');
		$this->load->model('location_types');
	}
	
	public function index(){
		echo "Location";
	}
	
	public function getGoogleLocations($lat,$lng,$radius = 1000,$type = '',$name = ''){
		$URL = $this->baseurl_list."location=".$lat.",".$lng."&radius=".$radius."&type=".$type."&name=".$name."&sensor=false&key=".$this->key;
		echo $URL."</br>";
		$content = file_get_contents($URL);
		$jsonString  = json_decode($content);
		if($jsonString->status == "OK"){
			for($i= 0 ;$i < count($jsonString->results);$i++){
				echo "Location : ".$i."</br>";
				
				if(isset($jsonString->results[$i]->id)){
					$locations["location_id"] = $jsonString->results[$i]->id;
					echo "Location ID : ".$locations["location_id"]."</br>";
				}
				
				if(isset($jsonString->results[$i]->geometry->location->lat)){
					$locations["lat"] = $jsonString->results[$i]->geometry->location->lat;
					echo "Lat : " .$locations["lat"]."</br>";
				}
				
				if(isset($jsonString->results[$i]->geometry->location->lng)){
					$locations["lng"] = $jsonString->results[$i]->geometry->location->lng;
					echo "Lng : " .$locations["lng"]."</br>";
				}
				
				if(isset($jsonString->results[$i]->name)){
					$locations["name"] = $jsonString->results[$i]->name;
					echo "Name : " .$locations["name"]."</br>";
				}
				
				if(isset($jsonString->results[$i]->icon)){
					$locations["icon"] = $jsonString->results[$i]->icon;
					echo "Icon : <image src=\"".$locations["icon"]."\"></image></br>";
				}
				
				if(isset($jsonString->results[$i]->rating)){
					$locations["rating"] = $jsonString->results[$i]->rating;
					echo "Rating : ".$locations["rating"]."</br>";
				}
				
				if(isset($jsonString->results[$i]->types)){
					$types = $jsonString->results[$i]->types;
					$locations["types"] = implode($types, ',');
					
					for($k = 0 ; $k < count($types);$k++){
						$types_ins['type_name'] = $types[$k]; 
						$types_ins['location_id'] = $locations['location_id'];
						$this->location_types->insertTypes($types_ins);
					}
					
				}
				
				if(isset($jsonString->results[$i]->reference)){
					$locations["reference"] = $jsonString->results[$i]->reference;
					
					echo "Reference : ".$locations["reference"]."</br>";
				}
				
				if(isset($jsonString->results[$i]->vicinity)){
					$locations["vicinity"] = $jsonString->results[$i]->vicinity;
					echo "Vicinity : ".$locations["vicinity"]."</br>";
				}
				
				if(isset($jsonString->results[$i]->events)){
					$events = $jsonString->results[$i]->events;
					for($j = 0;$j<coutn($vents);$j++){
						$event = $events[$j];
						$event["locatuon_id"] = $locations["location_id"];
						$this->events->insertEvent($event);
					}
					
				}
				
				if($this->location_detail->insertLocation($locations)){
					$this->getGooglePlaceDetail($locations['location_id']);
					echo "Insert DB success";
				}else{
					echo "Insert DB faile";
				}
				
				
				echo "</br></br>";
			}
			return $jsonString;
		}else{
			return $jsonString;
		}
	}
	
	
	public function getGooglePlaceDetail($location_id){
		
		$locations = get_object_vars($this->location_detail->getLocation($location_id));
		$reference = $locations['reference'];
		$url = $this->baseurl_detail.$reference."&sensor=true&key=".$this->key;
		$result = file_get_contents($url);
		$jsonString = json_decode($result);
		
		if($jsonString->status == "OK"){
			
			if(isset($jsonString->result->address_components)){
				$address_components = $jsonString->result->address_components;
				for($i=0;$i< count($address_components);$i++){
					$address_components_insert['location_id'] = $location_id;
					$address_components_insert['long_name'] = $address_components[$i]->long_name;
					$address_components_insert['short_name'] = $address_components[$i]->short_name;
					$address_components_insert['types'] = implode($address_components[$i]->types, ',');
					$this->address_components->insertComponent($address_components_insert);
				}
			}
			
			if(isset($jsonString->result->formatted_address)){
				$locations['formatted_address'] = $jsonString->result->formatted_address;
			}
			
			if(isset($jsonString->result->formatted_phone_number)){
				$locations['formatted_phone_number'] = $jsonString->result->formatted_phone_number;
			}
			
			if(isset($jsonString->result->international_phone_number)){
				$locations['international_phone_number'] = $jsonString->result->international_phone_number;
			}
			
			if(isset($jsonString->result->url)){
				$locations['url'] = $jsonString->result->url;
			}
			
			if(isset($jsonString->result->website)){
				$locations['website'] = $jsonString->result->website;
			}
			$this->location_detail->updateLocation($locations);
		}
		
	}
}